package Test;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class test1 {
    //力扣 136.只出现一次的数字
    public static void main(String[] args) {
        int[] nums = {1,2,3,2,1};
        //集合法
        int ret = method1(nums);
        System.out.println(ret);
        //异或法
        System.out.println(method2(nums));

    }

    private static int method2(int[] nums) {
        int ret = 0;
        for (int i = 0; i < nums.length; i++) {
            ret^=nums[i];
        }
        return ret;
    }

    public static int method1(int[] nums){
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if(set.contains(nums[i])){
                set.remove(nums[i]);
            }
            else{
                set.add(nums[i]);
            }
        }

        for (int i = 0; i < nums.length; i++) {
            if(set.contains(nums[i])){
                return nums[i];
            }

        }
        return -1;
    }
}
